package pers.baiban.sqltoword.handler.impl;

import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import pers.baiban.sqltoword.handler.AbstractTableProcessor;
import pers.baiban.sqltoword.handler.TableProcessor;
import pers.baiban.sqltoword.util.TableUtil;

import java.util.List;
import java.util.stream.Collectors;

import static pers.baiban.sqltoword.util.TableUtil.Functors.CTTC;
import static pers.baiban.sqltoword.util.TableUtil.Functors.TCPR;

/**
 * 表格内所有单元格处理器
 *
 * @author 强子初
 * @since 2023/11/29
 */
@Component
@Order(5)
public class TableAllCellProcessor extends AbstractTableProcessor implements TableProcessor {

    @Override
    public void doHandler() {
        if (needUpdate(param.getTableCellAlignment())) {
            getParagraph().forEach(it -> it.setAlignment(param.getTableCellAlignment()));
        }

        if (needUpdate(param.getTableCellSTVerticalJc())) {
            getTcPr().forEach(it -> it.addNewVAlign().setVal(param.getTableCellSTVerticalJc()));
        }
    }

    /**
     * 获取表格单元格文本
     */
    private List<XWPFParagraph> getParagraph() {
        return cells.stream()
                .map(cell -> cell.getParagraphArray(0))
                .collect(Collectors.toList());
    }

    private List<CTTcPr> getTcPr() {
        return TableUtil.to(cells, CTTC.andThen(TCPR));
    }
}